/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package primephysique;

import java.awt.Component;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Kyle
 */
public class FeedBackManage extends javax.swing.JPanel {

    /**
     * Creates new form ProgramManage
     */
    public FeedBackManage() {
        initComponents();
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String query = "SELECT FullName FROM Trainee";
            rs = st.executeQuery(query);
            Vector brData = new Vector();
            while (rs.next()) {
                brData.add(rs.getString(1));
            }
            cbbSender.setModel(new DefaultComboBoxModel(brData));
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showTable() {
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

          //  int s = cbbSender.getSelectedIndex();
            //get TraineeID
                String s="";
                String name=cbbSender.getSelectedItem().toString().trim();
                String sq = "SELECT TraineeID FROM Trainee WHERE FullName ='" + name + "'";
                ResultSet tid = con.createStatement().executeQuery(sq);

                tid.next();
                s=tid.getString("TraineeID");
                //end get
            String query = "select FeedBack.Title, Trainee.FullName, FeedBack.[Content],Feedback.CreateDate from FeedBack "
                    + " join Trainee ON (FeedBack.TraineeID = Trainee.TraineeID )"
                    + " where ( Trainee.TraineeID ='" + s + "')";
            rs = st.executeQuery(query);
            rsmd = rs.getMetaData();
            Vector<String> columnName = new Vector<String>();
            Vector<String> columnData;
            for (int i = 0; i < rsmd.getColumnCount(); i++) {
                columnName.add(rsmd.getColumnName(i + 1));
            }
            // set table column's Name
            model = new DefaultTableModel();
            int cc = rsmd.getColumnCount();
            for (int i = 0; i < cc; i++) {
                model.addColumn(columnName.get(i));
            }

            while (rs.next()) {
                columnData = new Vector<String>();
                for (int i = 0; i < cc; i++) {
                    columnData.add(rs.getString(i + 1));
                    //     System.out.println(rs.getString(i + 1));
                }
                model.addRow(columnData);
                //         columnData.clear();
            }
            tbListFeedBack.setModel(model);
            scpListFeedBack.setViewportView(tbListFeedBack);
            con.close();


        } catch (SQLException ex) {
            Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lbID = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        lbName = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnView = new javax.swing.JButton();
        txtContent = new javax.swing.JTextField();
        lbContent = new javax.swing.JLabel();
        lbSende = new javax.swing.JLabel();
        dchSendDate = new com.toedter.calendar.JDateChooser();
        cbbSender = new javax.swing.JComboBox();
        lbSendDate = new javax.swing.JLabel();
        pnlViewTable = new javax.swing.JPanel();
        scpListFeedBack = new javax.swing.JScrollPane();
        tbListFeedBack = new javax.swing.JTable();

        jPanel1.setName("Program Manager");

        lbID.setText("ID");

        lbName.setText("Title");

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");

        btnView.setText("View");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        lbContent.setText("Content");

        lbSende.setText("Sender");

        cbbSender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lbSendDate.setText("Send Date");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbID)
                    .addComponent(lbName)
                    .addComponent(lbContent)
                    .addComponent(lbSende)
                    .addComponent(lbSendDate))
                .addGap(44, 44, 44)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtID, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                    .addComponent(txtContent)
                    .addComponent(txtTitle)
                    .addComponent(dchSendDate, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbbSender, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(87, 87, 87)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnView, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(130, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbID)
                    .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnUpdate)
                    .addComponent(lbName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAdd)
                        .addGap(9, 9, 9)
                        .addComponent(btnView))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lbContent)
                            .addComponent(txtContent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbSende)
                            .addComponent(cbbSender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancel)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(lbSendDate)
                        .addComponent(dchSendDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 24, Short.MAX_VALUE))
        );

        tbListFeedBack.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        scpListFeedBack.setViewportView(tbListFeedBack);

        javax.swing.GroupLayout pnlViewTableLayout = new javax.swing.GroupLayout(pnlViewTable);
        pnlViewTable.setLayout(pnlViewTableLayout);
        pnlViewTableLayout.setHorizontalGroup(
            pnlViewTableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scpListFeedBack, javax.swing.GroupLayout.DEFAULT_SIZE, 520, Short.MAX_VALUE)
        );
        pnlViewTableLayout.setVerticalGroup(
            pnlViewTableLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlViewTableLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scpListFeedBack, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlViewTable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pnlViewTable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.getAccessibleContext().setAccessibleName("Program Manager");
    }// </editor-fold>//GEN-END:initComponents

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if (txtID.getText() == null || "".equals(txtID.getText())) {
            JOptionPane.showMessageDialog(this, "input in ID text");
            return;
        }
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement();
            st.executeUpdate("delete from Teacher where TeacherID = " + Integer.parseInt(txtID.getText()));
            JOptionPane.showMessageDialog(this, " Delete Completed");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex);
        }
        finally {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(FeedBackManage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }        
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        // TODO add your handling code here:
        showTable();
    }//GEN-LAST:event_btnViewActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
      //Check input data
        
        if ("".equals(txtTitle.getText())) {
            JOptionPane.showMessageDialog(this, "Title is empty !");
            return;
        }
        if ("".equals(txtContent.getText())) {
            JOptionPane.showMessageDialog(this, "Content is empty !");
            return;
        }
        if ("".equals(dchSendDate.getDate().toString())) {
            JOptionPane.showMessageDialog(this, "Send Date is empty !");
            return;
        }
        try {
            // TODO add your handling code here:
          //  java.util.Date cdate = new java.util.Date();
          //  Timestamp date = new java.sql.Timestamp(cdate.getTime());
          //  DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
            java.util.Date selectedDate = null;            
            selectedDate = new java.util.Date(dchSendDate.getDate().getTime());    
            Timestamp date =new Timestamp(selectedDate.getTime());
            
            try {
                cdb = new ConnectDB();
                con = cdb.getConnection();
                //get TraineeID
                String sid="";
                String name=cbbSender.getSelectedItem().toString().trim();
                String sq = "SELECT TraineeID FROM Trainee WHERE FullName ='" + name + "'";
                ResultSet tid = con.createStatement().executeQuery(sq);

                tid.next();
                sid=tid.getString("TraineeID");
                //end get
                st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                st.executeUpdate("insert into FeedBack( Title, Content,TraineeID, CreateDate)"
                        + "values('" + txtTitle.getText()
                        + "','" + txtContent.getText()
                        + "'," + sid
                        + ",'" + date + "')");
                JOptionPane.showMessageDialog(this, " Add Completed");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex);
            }
            txtID.setText("");
            txtTitle.setText("");
            txtContent.setText("");
        } catch (Exception ex) {
            Logger.getLogger(FeedBackManage.class.getName()).log(Level.SEVERE, null, ex);
        }
         finally {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(FeedBackManage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }   
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        if (txtID.getText() == null || "".equals(txtID.getText())) {
            JOptionPane.showMessageDialog(this, " Input in ID text");
            return;
        }
        if ("".equals(txtTitle.getText())) {
            JOptionPane.showMessageDialog(this, "Title is empty !");
            return;
        }
        if ("".equals(txtContent.getText())) {
            JOptionPane.showMessageDialog(this, "Content is empty !");
            return;
        }
        if ("".equals(dchSendDate.getDate().toString())) {
            JOptionPane.showMessageDialog(this, "Send date is empty !");
            return;
        }
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            st.executeUpdate("update FeedBack "
                    + "set Title = '" + txtTitle.getText() + "', "
                    + "[Content] = '" + txtContent.getText() + "', "
                    + "TraineeID = " + cbbSender.getSelectedItem().toString() + ","
                    + "CreateDate = " + dchSendDate.getDate().getTime() + " "
                    + "where TeacherID = " + Integer.parseInt(txtID.getText()));
            JOptionPane.showMessageDialog(this, "Update Completed");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex);
        }
         finally {
            try {
                con.close();
            } catch (SQLException ex) {
                Logger.getLogger(FeedBackManage.class.getName()).log(Level.SEVERE, null, ex);
            }
        }   
        txtID.setText("");
        txtTitle.setText("");
        txtContent.setText("");
        
    }//GEN-LAST:event_btnUpdateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JButton btnView;
    private javax.swing.JComboBox cbbSender;
    private com.toedter.calendar.JDateChooser dchSendDate;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbContent;
    private javax.swing.JLabel lbID;
    private javax.swing.JLabel lbName;
    private javax.swing.JLabel lbSendDate;
    private javax.swing.JLabel lbSende;
    private javax.swing.JPanel pnlViewTable;
    private javax.swing.JScrollPane scpListFeedBack;
    private javax.swing.JTable tbListFeedBack;
    private javax.swing.JTextField txtContent;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables
    private ConnectDB cdb = null;
    private Connection con;
    private Statement st;
    private ResultSet rs;
    private ResultSetMetaData rsmd;
    private DefaultTableModel model = new DefaultTableModel();
}
